﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;
using Microsoft.AspNetCore.Http;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 配置表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksConfigService), ServiceLifetime = LifeTime.Transient)]
    public class CksConfigService : BaseService<CksConfig>, ICksConfigService
    {
        /// <summary>
        /// 查询配置表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksConfigDto> GetList(CksConfigQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<CksConfig, CksConfigDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="ConfigId"></param>
        /// <returns></returns>
        public CksConfig GetInfo(int ConfigId)
        {
            var response = Queryable()
                .Where(x => x.ConfigId == ConfigId)
                .First();

            return response;
        }

        /// <summary>
        /// 添加配置表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksConfig AddCksConfig(CksConfig model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改配置表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksConfig(CksConfig model)
        {
            
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksConfig> QueryExp(CksConfigQueryDto parm)
        {
            var predicate = Expressionable.Create<CksConfig>();

            return predicate;
        }

        public CksConfig GetSysConfigByKey(string key)
        {
            return Queryable().First(f => f.ConfigKey == key);
        }
    }
}